package cn.jn.lhm.teacher.handler;

import cn.jn.lhm.teacher.entity.EduVideo;
import cn.jn.lhm.teacher.feign.VodClient;
import cn.jn.lhm.teacher.service.EduVideoService;
import cn.jn.lhm.utils.R;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/2 - 21:28
 */

@RestController
@RequestMapping("/video")
public class EduVideoHandler {

    @Resource
    private EduVideoService eduVideoService;

    @Resource
    private VodClient vodClient;

    @GetMapping("/get/{id}")
    public R getVideoById(@PathVariable("id")String id){
        EduVideo eduVideo = eduVideoService.getVideoById(id);
        return R.ok().data("data",eduVideo);
    }

    @PostMapping("/add")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }


    @PostMapping("/update")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateVideo(eduVideo);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable("id")String id){
        //根据小节Id获取视频Id
        EduVideo eduVideo = eduVideoService.getVideoById(id);
        if(!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
            vodClient.deleteVodById(eduVideo.getVideoSourceId());
        }
        eduVideoService.deleteById(id);
        return R.ok();
    }


}
